import urllib.error , urllib.request
import re
import webbrowser
import datetime
import time
import os
import requests

def  isConnected():
    try:
       connect_web = requests.get("https://tv.cctv.com/lm/xwlb/index.shtml",timeout=2)
    except:
       return False
    return True
  
  
def links():   #获取新闻联播地址
#connect to a URL
    website = urllib.request.urlopen('https://tv.cctv.com/lm/xwlb/index.shtml')
#read html code
    html = website.read()
#use re.findall to get all the links
    links = re.findall('"((http|ftp)s?://.*?)"', html.decode('utf-8'))
    #print (links)
    f = open('link_sum.ini')
    link_sum = int(f.read())  
    a = links[link_sum]
#file = open('xwlb.txt','w')
    b = ''.join(a)
    c = b.rstrip('http')
#print(b)
    #print(c)
    list1=[]
    sum = 0
    for i in links:
        #sum = sum +1
        sum1 = str(sum)
        i  = ''.join(i)
        ii = i.rstrip('http')
        list1 = sum1 + '. ' + ''.join(ii)
        #list1.append()
        print(list1)
        sum = sum +1
    return c

#file.write(b.rstrip('http'))
#time.sleep(2)
#os.rename('xwlb.txt', '今天的新闻联播地址.txt')

#w = 0
#while w==0:
def get_time():  #获取时间，并判断是否到14：00
       now = datetime.datetime.now()
       #print ("当前系统日期和时间是: ")
       #print (now.strftime("%Y-%m-%d %H:%M:%S"))
       hour = now.strftime("%H")
       minute = now.strftime("%M")
       second = now.strftime("%S")
       '''
       if hour == '11' and minute == '00':
          times = 'done'
          return times
       '''
       times = 1400
       if hour == '14' and minute == '00':
          
          return times

       ############调试开始###########

       #return times

       ############调试结束###########
       #print(hour)
       #time.sleep(10)
       
def open_browser(url):   #使用默认浏览器在新窗口打开链接
    webbrowser.open_new_tab(url)


#links()
while 1:
    print('start')
    print(isConnected())
    ############调试模式####################
    '''
    url=links()
    print('done')
    open_browser(url)
    time.sleep(60)
    '''
    ###########调试模式结束################

    
    if str(isConnected()) == 'True':
        print('network is ok')
        if get_time() == 1400:
            print('time is ok')
            #links()
            url=links()
            print('open the web')
            open_browser(url)
            time.sleep(60)
    else:
         print('')
        

'''
('https://tv.cctv.com/2022/03/15/VIDEdSeGf7lLD7qg79wEH1nW220315.shtml', 'http'), 
('https://tv.cctv.com/2022/03/15/VIDEdSeGf7lLD7qg79wEH1nW220315.shtml', 'http'), (
'https://tv.cctv.com/2022/03/15/VIDEOQ6SB41EzECPRDnjG01d220315.shtml', 'http')
, ('https://tv.cctv.com/2022/03/15/VIDEOQ6SB41EzECPRDnjG01d220315.shtml', 'http')
, ('https://tv.cctv.com/2022/03/15/VIDEGPVI6OuRqJuBsNROHjzq220315.shtml', 'http'), 
('https://tv.cctv.com/2022/03/15/VIDEGPVI6OuRqJuBsNROHjzq220315.shtml', 'http'), 
('https://tv.cctv.com/2022/03/15/VIDEVkz1PDWHBmvZQO3lLGx9220315.shtml', 'http'),
 ('https://tv.cctv.com/2022/03/15/VIDEVkz1PDWHBmvZQO3lLGx9220315.shtml', 'http'), 
 ('https://tv.cctv.com/2022/03/15/VIDEYeYvwmgLruYK76XpEvCS220315.shtml', 'http')
 , ('https://tv.cctv.com/2022/03/15/VIDEYeYvwmgLruYK76XpEvCS220315.shtml', 'http'), 
 ('https://tv.cctv.com/2022/03/15/VIDEomcKtAzBbLoCocUcCQoC220315.shtml', 'http')
 , ('https://tv.cctv.com/2022/03/15/VIDEomcKtAzBbLoCocUcCQoC220315.shtml', 'http'),
  ('https://tv.cctv.com/2022/03/15/VIDEv1gITbiOOCysWbqGJNTb220315.shtml', 'http'),
   ('https://tv.cctv.com/2022/03/15/VIDEv1gITbiOOCysWbqGJNTb220315.shtml', 'http'), 
   ('https://tv.cctv.com/2022/03/15/VIDECIjulOwQPOlwVQKAmXNm220315.shtml', 'http')
   , ('https://tv.cctv.com/2022/03/15/VIDECIjulOwQPOlwVQKAmXNm220315.shtml', 'http'), 
('https://tv.cctv.com/2022/03/15/VIDE8nZex2NH6WqFf6WYQwsO220315.shtml', 'http'),
 ('https://tv.cctv.com/2022/03/15/VIDE8nZex2NH6WqFf6WYQwsO220315.shtml', 'http'), 
 ('https://tv.cctv.com/2022/03/15/VIDECEMwmyPkZ3n6m5J8p08q220315.shtml', 'http')
 , ('https://tv.cctv.com/2022/03/15/VIDECEMwmyPkZ3n6m5J8p08q220315.shtml', 'http'),
  ('https://tv.cctv.com/2022/03/15/VIDEU9vAG8dFKUsbkqPh4ekL220315.shtml', 'http'),
   ('https://tv.cctv.com/2022/03/15/VIDEU9vAG8dFKUsbkqPh4ekL220315.shtml', 'http'),
 ('https://tv.cctv.com/2022/03/15/VIDEW2RmOXQX0ECMK6I6MfwU220315.shtml', 'http'), 
 ('https://tv.cctv.com/2022/03/15/VIDEW2RmOXQX0ECMK6I6MfwU220315.shtml', 'http'),
  ('https://tv.cctv.com/2022/03/15/VIDEu3buL92Py1frWC6jDpot220315.shtml', 'http'), 
  ('https://tv.cctv.com/2022/03/15/VIDEu3buL92Py1frWC6jDpot220315.shtml', 'http')
  , ('https://tv.cctv.com/2022/03/15/VIDEmrWTqQJbr8yOueoTVp0S220315.shtml', 'http'), 
  ('https://tv.cctv.com/2022/03/15/VIDEmrWTqQJbr8yOueoTVp0S220315.shtml', 'http'), 
  ('http://news.cctv.com/program/xwlb/', 'http')
  , ('http://news.cntv.cn/program/xwlb/', 'http'), 
  ('http://cctv.cntv.cn/lm/xinwenlianbo/', 'http'), 
  ('http://cctv.cntv.cn/lm/xinwenlianbo/', 'http'), 
  ('http://news.cctv.com/program/xwlb/', 'http'), 
  ('http://news.cntv.cn/program/xwlb/', 'http'), 
  ('http://cctv.cntv.cn/lm/xinwenlianbo/', 'http'),
   ('http://cctv.cntv.cn/lm/xinwenlianbo/', 'http'), 
   ('https://tv.cctv.com/lm/wjxw/index.shtml', 'http'),
    ('https://tv.cctv.com/lm/wjxw/index.shtml', 'http')
    , ('https://tv.cctv.com/lm/zwtx/index.shtml', 'http'),
     ('https://tv.cctv.com/lm/zwtx/index.shtml', 'http'),
      ('https://tv.cctv.com/lm/jrsf/index.shtml', 'http'), 
      ('https://tv.cctv.com/lm/jrsf/index.shtml', 'http'),
  ('https://tv.cctv.com/lm/gtgz/index.shtml', 'http'), 
  ('https://tv.cctv.com/lm/gtgz/index.shtml', 'http'),
  ('https://tv.cctv.com/2022/03/15/VIDEdSeGf7lLD7qg79wEH1nW220315.shtml', 'http')
  , ('https://tv.cctv.com/2022/03/15/VIDEdSeGf7lLD7qg79wEH1nW220315.shtml', 'http'), 
  ('https://tv.cctv.com/2022/03/15/VIDEOQ6SB41EzECPRDnjG01d220315.shtml', 'http'),
   ('https://tv.cctv.com/2022/03/15/VIDEOQ6SB41EzECPRDnjG01d220315.shtml', 'http'),
    ('https://tv.cctv.com/2022/03/15/VIDEGPVI6OuRqJuBsNROHjzq220315.shtml', 'http'),
     ('https://tv.cctv.com/2022/03/15/VIDEGPVI6OuRqJuBsNROHjzq220315.shtml', 'http'), 
     ('https://tv.cctv.com/2022/03/15/VIDEVkz1PDWHBmvZQO3lLGx9220315.shtml', 'http'), 
     ('https://tv.cctv.com/2022/03/15/VIDEVkz1PDWHBmvZQO3lLGx9220315.shtml', 'http'),
   ('https://tv.cctv.com/2022/03/15/VIDEYeYvwmgLruYK76XpEvCS220315.shtml', 'http')
   , ('https://tv.cctv.com/2022/03/15/VIDEYeYvwmgLruYK76XpEvCS220315.shtml', 'http'), 
   ('https://tv.cctv.com/2022/03/15/VIDEomcKtAzBbLoCocUcCQoC220315.shtml', 'http'),
    ('https://tv.cctv.com/2022/03/15/VIDEomcKtAzBbLoCocUcCQoC220315.shtml', 'http'), 
    ('https://tv.cctv.com/2022/03/15/VIDEv1gITbiOOCysWbqGJNTb220315.shtml', 'http'), 
    ('https://tv.cctv.com/2022/03/15/VIDEv1gITbiOOCysWbqGJNTb220315.shtml', 'http'), 
    ('https://tv.cctv.com/2022/03/15/VIDECIjulOwQPOlwVQKAmXNm220315.shtml', 'http'), 
    ('https://tv.cctv.com/2022/03/15/VIDECIjulOwQPOlwVQKAmXNm220315.shtml', 'http'), 
    ('https://tv.cctv.com/2022/03/15/VIDE8nZex2NH6WqFf6WYQwsO220315.shtml', 'http'), ('https://tv.cctv.com/2022/03/15/VIDE8nZex2NH6WqFf6WYQwsO220315.shtml', 'http'), 
    ('https://tv.cctv.com/2022/03/15/VIDECEMwmyPkZ3n6m5J8p08q220315.shtml', 'http'), ('https://tv.cctv.com/2022/03/15/VIDECEMwmyPkZ3n6m5J8p08q220315.shtml', 'http'), ('https://tv.cctv.com/2022/03/15/VIDEU9vAG8dFKUsbkqPh4ekL220315.shtml', 'http'), ('https://tv.cctv.com/2022/03/15/VIDEU9vAG8dFKUsbkqPh4ekL220315.shtml', 'http'), ('https://tv.cctv.com/2022/03/15/VIDEW2RmOXQX0ECMK6I6MfwU220315.shtml', 'http'), ('https://tv.cctv.com/2022/03/15/VIDEW2RmOXQX0ECMK6I6MfwU220315.shtml', 'http'), ('https://tv.cctv.com/2022/03/15/VIDEu3buL92Py1frWC6jDpot220315.shtml', 'http'), ('https://tv.cctv.com/2022/03/15/VIDEu3buL92Py1frWC6jDpot220315.shtml', 'http'), ('https://tv.cctv.com/2022/03/15/VIDEmrWTqQJbr8yOueoTVp0S220315.shtml', 'http'), ('https://tv.cctv.com/2022/03/15/VIDEmrWTqQJbr8yOueoTVp0S220315.shtml', 'http'), ('http://news.cctv.com/program/xwlb/', 'http'), ('http://news.cntv.cn/program/xwlb/', 'http'), ('http://cctv.cntv.cn/lm/xinwenlianbo/', 'http'), ('http://cctv.cntv.cn/lm/xinwenlianbo/', 'http'), ('http://news.cctv.com/program/xwlb/', 'http'), ('http://news.cntv.cn/program/xwlb/', 'http'), ('http://cctv.cntv.cn/lm/xinwenlianbo/', 'http'), ('http://cctv.cntv.cn/lm/xinwenlianbo/', 'http'), ('https://tv.cctv.com/lm/wjxw/index.shtml', 'http'), ('https://tv.cctv.com/lm/wjxw/index.shtml', 'http'), ('https://tv.cctv.com/lm/zwtx/index.shtml', 'http'), ('https://tv.cctv.com/lm/zwtx/index.shtml', 'http'), ('https://tv.cctv.com/lm/jrsf/index.shtml', 'http'), ('https://tv.cctv.com/lm/jrsf/index.shtml', 'http'), ('https://tv.cctv.com/lm/gtgz/index.shtml', 'http'), ('https://tv.cctv.com/lm/gtgz/index.shtml', 'http'), ('https://tv.cctv.com/lm/dfsk/index.shtml', 'http'), ('https://tv.cctv.com/lm/dfsk/index.shtml', 'http'), ('https://tv.cctv.com/lm/gjsx/index.shtml', 'http'), ('https://tv.cctv.com/lm/gjsx/index.shtml', 'http'), ('https://tv.cctv.com/lm/hqsx/index.shtml', 'http'), ('https://tv.cctv.com/lm/hqsx/index.shtml', 'http'), ('https://tv.cctv.com/lm/24xiaoshi/index.shtml', 'http'), ('https://tv.cctv.com/lm/24xiaoshi/index.shtml', 'http'), ('https://tv.cctv.com/lm/sdkmfbt/index.shtml', 'http'), ('https://tv.cctv.com/lm/sdkmfbt/index.shtml', 'http'), ('https://tv.cctv.com/lm/zgjjdjt/', 'http'), ('https://tv.cctv.com/lm/zgjjdjt/', 'http'), ('https://tv.cctv.com/lm/index.shtml', 'http'), ('https://m.cctv.com/', 'http'), ('https://tv.cctv.com/m/', 'http')
   ('https://tv.cctv.com/lm/dfsk/index.shtml', 'http'), ('https://tv.cctv.com/lm/dfsk/index.shtml', 'http'), ('https://tv.cctv.com/lm/gjsx/index.shtml', 'http'), ('https://tv.cctv.com/lm/gjsx/index.shtml', 'http'), ('https://tv.cctv.com/lm/hqsx/index.shtml', 'http'), ('https://tv.cctv.com/lm/hqsx/index.shtml', 'http'), ('https://tv.cctv.com/lm/24xiaoshi/index.shtml', 'http'), ('https://tv.cctv.com/lm/24xiaoshi/index.shtml', 'http'), ('https://tv.cctv.com/lm/sdkmfbt/index.shtml', 'http'), ('https://tv.cctv.com/lm/sdkmfbt/index.shtml', 'http'), ('https://tv.cctv.com/lm/zgjjdjt/', 'http'), ('https://tv.cctv.com/lm/zgjjdjt/', 'http'), ('https://tv.cctv.com/lm/index.shtml', 'http'), ('https://m.cctv.com/', 'http'), 
('https://tv.cctv.com/m/', 'http')
'''
